/**
 * 调用方式 this.$httpPost(url,params,function(res){});
 * @param {Object} Vue
 * @param {Object} option
 */
exports.install = function(Vue, option) {

    /**
     * 获取当前时间
     */
    Vue.prototype.$getNowFormatDate = function() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();

        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate + " " + hour + ':' + minute + ':' +
            second;
        return currentdate;
    };


    /**
     * 判断是pc登录还是移动端app登录
     */
    Vue.prototype.$isPC = function() {
        var userAgentInfo = navigator.userAgent;
        var Agents = ["Android", "iPhone",
            "SymbianOS", "Windows Phone",
            "iPad", "iPod"
        ];
        var flag = true;
        for (var v = 0; v < Agents.length; v++) {
            if (userAgentInfo.indexOf(Agents[v]) > 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    /**
     * 获取前一个页面
     */
    Vue.prototype.$getBeforePage = function() {
        let pages = getCurrentPages(); //当前页
        let beforePage = pages[pages.length - 2]; //上个页面
        let beforePageObj = beforePage;
        // #ifdef H5
        beforePageObj = beforePage
        // #endif
        // #ifndef H5
        beforePageObj = beforePage.$vm
        // #endif
        return beforePageObj;
    }


    /**
     * 清查缓存和登录信息
     */
    Vue.prototype.$clearLoginInfo = function() {
        uni.removeStorageSync('LOGIN_RESULT')
    }

    /**
     * 清除缓存
     */
    Vue.prototype.$clearCache = function() {
        const ROOT_URL = uni.getStorageSync('ROOT_URL');
        const LOGIN_USER_INFO = uni.getStorageSync('LOGIN_USER_INFO')
        const COOKIES = uni.getStorageSync('COOKIES');
        const QH_API = uni.getStorageSync('QH_API')

        uni.clearStorageSync()

        uni.setStorageSync('ROOT_URL', ROOT_URL)
        uni.setStorageSync('LOGIN_USER_INFO', LOGIN_USER_INFO)
        uni.setStorageSync('COOKIES', COOKIES)
        uni.setStorageSync('QH_API', QH_API)
    }

    /**
     * 计算可视区域数据
     */
    Vue.prototype.$getShowList = function(e) {
        let tmpMusicList = []
        for (let i = 0; i < this.$cache.virtualListConfig.beforeItemNum; i++) {
            tmpMusicList.push({
                id: "music_tmp_" + i,
                title: "虚拟歌曲" + i,
                additional: {
                    song_audio: {},
                    song_rating: {},
                    song_tag: {}
                }
            })
        }
        this.$cache.virtualListConfig.allMusicList = [...tmpMusicList, ...this.allMusicList]

        this.$cache.virtualListConfig.scrollTop = e.detail.scrollTop
        //可视区域第一条数据的索引
        this.$cache.virtualListConfig.startIndex = Math.floor(this.$cache.virtualListConfig.scrollTop / this
            .$cache.virtualListConfig
            .itemHeight);
        //可视区域最后一条数据的后面那条数据的索引
        this.$cache.virtualListConfig.endIndex = this.$cache.virtualListConfig.startIndex + this.$cache
            .virtualListConfig
            .showNum;
        let change = false
        //可视区域显示的数据，即最后要渲染的数据。实际的数据索引是从this.startIndex到this.endIndex-1
        if (this.$cache.virtualListConfig.startIndex != this.$cache.virtualListConfig.oldIndex.startIndex ||
            this.$cache.virtualListConfig.endIndex != this.$cache.virtualListConfig.oldIndex.endIndex) {
            let beforeItemNum = this.$cache.virtualListConfig.beforeItemNum - this.$cache.virtualListConfig
                .startIndex;
            this.musicList = this.$cache.virtualListConfig.allMusicList.slice(this.$cache.virtualListConfig.startIndex, this
                .$cache.virtualListConfig.endIndex)

            this.$cache.virtualListConfig.oldIndex.startIndex = this.$cache.virtualListConfig.startIndex
            this.$cache.virtualListConfig.oldIndex.endIndex = this.$cache.virtualListConfig.endIndex
            change = true
        }
        //在这需要获得一个可以被itemHeight整除的数来作为item的偏移量，这样随机滑动时第一条数据都是完整显示的
        // this.$cache.virtualListConfig.top = this.$cache.virtualListConfig.scrollTop - (this.$cache
        //     .virtualListConfig.scrollTop % this
        //     .$cache.virtualListConfig.itemHeight);
        this.$cache.virtualListConfig.top = this.$cache.virtualListConfig.scrollTop - (this.$cache
                .virtualListConfig.scrollTop % this
                .$cache.virtualListConfig.itemHeight) - this
            .$cache.virtualListConfig.itemHeight * (this.$cache.virtualListConfig.beforeItemNum);


        let lastIndex = this.$cache.virtualListConfig.showNum - 1
        if (lastIndex >= this.musicList.length) {
            lastIndex = this.musicList.length - 1
        }
        // if (change) {
        //     console.log('计算可视区域数据', [this.$cache.virtualListConfig.startIndex, this.
        //             $cache.virtualListConfig.endIndex
        //         ], this.$cache.virtualListConfig, this.musicList[0].title, ' || ',
        //         this.musicList[lastIndex].title)
        // }

    }

}
